import {
	VUE_APP_API_URL
} from '@/utils/config.js'

function req(obj) {
	console.log(obj)
	return new Promise((resolve, reject) => {
		const HOST = VUE_APP_API_URL
		var method = obj.method || "GET";
		var url = HOST + obj.url || "";
		var data = obj.data || {};
		var header = obj.header || {
			// 'Content-Type': 'application/x-www-form-urlencoded',
		};
	
		var success = obj.success; // 成功回调函数
		var fail = obj.fail; //表示失败后，要执行的回调函数
		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			success: ((res) => {
				if (res.statusCode === 403 || res.statusCode === 401) {
					// 错误处理，返回登录页
					// uni.reLaunch({url:'/pages/login/index'})
				} else if (res.statusCode === 200) {
					if (res.data.code == -5 || res.data.msg == 'token不能为空') {
						uni.navigateTo({
							url: '/pages/index/index?PageCur=my'
						})
					}
					resolve(res.data)
				}
			}),
			fail: ((err) => {
				reject(err)
			})
		})
	})
}
export default req
